package com.cskaoyan.controller;

import com.cskaoyan.bean.BaseData;
import com.cskaoyan.bean.CskaoyanmallComment;
import com.cskaoyan.bean.CskaoyanmallGoods;
import com.cskaoyan.bean.bo.GoodsCreateBo;
import com.cskaoyan.bean.vo.BaseRespVo;
import com.cskaoyan.bean.vo.goods.GoodsRequestVo;
import com.cskaoyan.bean.vo.goods.ResVo;
import com.cskaoyan.service.GoodsService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created with IntelliJ IDEA.
 *
 * @author wangguangwu
 * @Auther: wangguangwu
 * @Date: 2021/04/08/22:14
 * @Description: 商品管理的控制层
 */

@RestController
@RequestMapping("admin")
public class GoodsController {

    @Autowired
    GoodsService goodsService;


    /**
     * 商品列表
     * 完成
     * @param goodsRequestVo
     * @return BaseRespVo
     */
    @RequiresPermissions("admin:goods:list")
    @RequestMapping("/goods/list")
    public BaseRespVo list(@ModelAttribute GoodsRequestVo goodsRequestVo) {
        BaseData baseData = goodsService.qureyGoods(goodsRequestVo);
        return BaseRespVo.success(baseData);
    }


    /**
     * 商品上架
     * 完成
     * @param goodsCreateBo
     * @return BaseRespVo
     */
    @RequiresPermissions("admin:goods:create")
    @RequestMapping("/goods/create")
    public BaseRespVo goodsCreate(@RequestBody GoodsCreateBo goodsCreateBo) {
        int res = goodsService.createGoods(goodsCreateBo);
        System.out.println(res);
        if (res == 10000) {
            return BaseRespVo.fail(401);
        } else if (res == 20000) {
            return BaseRespVo.fail(611);
        } else {
            System.out.println(goodsCreateBo);
            return BaseRespVo.success();
        }
    }

    /**
     * 商品更新
     * 完成
     * @param goodsUpdateBo
     * @return BaseRespVo
     */
    @RequiresPermissions("admin:goods:update")
    @RequestMapping("/goods/update")
    public BaseRespVo goodsUpdate(@RequestBody GoodsCreateBo goodsUpdateBo) {
        int res = goodsService.updateGoods(goodsUpdateBo);
        System.out.println(res);
        if (res == 0) {
            return BaseRespVo.fail(401);
        } else {
            System.out.println(goodsUpdateBo);
            return BaseRespVo.success();
        }
    }

    /**
     * 商品详情
     * 完成
     * @param id
     * @return
     */
    @RequiresPermissions("admin:goods:detail")
    @RequestMapping("/goods/detail")
    public BaseRespVo goodsDetailList(Integer id) {
        return BaseRespVo.success(goodsService.queryGoodsDetail(id));
    }

    /**
     * 商品品牌
     * 完成
     * @return BaseRespVo
     */
    @RequiresPermissions("admin:goods:create")
    @RequestMapping("goods/catAndBrand")
    public BaseRespVo goodsCatAndBrand() {
        return BaseRespVo.success(goodsService.queryCatAndBrand());
    }

    /**
     * 商品删除
     * 完成
     * @param goods
     * @return BaseRespVo
     */
    @RequiresPermissions("admin:goods:delete")
    @RequestMapping("goods/delete")
    public BaseRespVo goodsDelete(@RequestBody CskaoyanmallGoods goods) {
        goodsService.deleteGoods(goods);
        return BaseRespVo.success();
    }

    /**
     * 评论列表
     * 完成
     * @param page
     * @param limit
     * @param sort
     * @param order
     * @return
     */
    @RequiresPermissions("admin:comment:list")
    @RequestMapping("/comment/list")
    public BaseRespVo commentList(Integer page, Integer limit, String sort, String order) {
        return BaseRespVo.success(goodsService.queryComments(page,limit,sort,order));
    }

    /**
     * 评论删除
     * 完成
     * @param comment
     * @return
     */
    @RequiresPermissions("admin:comment:delete")
    @RequestMapping("/comment/delete")
    public ResVo commentDelete(CskaoyanmallComment comment){
        goodsService.deleteComment(comment);
        return ResVo.success();
    }
}
